<?php

/**
 *
 */
class UploadHelper {

    /**
     * allow admin save
     * @param CUploadedFile $uploadFile
     * @return mixed boolean if upload fail
     *
     */
    public static function saveAvatar($uploadFile) {
        //allow extensions?
        if (!in_array($uploadFile->extensionName, array('png', 'jpg', 'jpeg', 'bmp', 'gif'))) {
            return false;
        }

        $fileName = time() . '.' . $uploadFile->extensionName;
        if ($uploadFile->saveAs(AVATAR_PATH . $fileName)) {
            //save file successfully
            return $fileName;
        }

        return false;
    }

    /**
     * allow user save
     * @param CUploadedFile $uploadFile
     * @return mixed boolean if upload fail
     *
     */
    public static function saveLogo($uploadFile) {
        //allow extensions?
        if (!in_array($uploadFile->extensionName, array('png', 'jpg', 'jpeg', 'bmp', 'gif', 'PNG', 'JPG', 'JPEG', 'BMP', 'GIF'))) {
            return false;
        }

        $fileName = time() . '.' . $uploadFile->extensionName;
        if ($uploadFile->saveAs(LOGO_PATH . $fileName)) {
            //save file successfully
            return $fileName;
        }

        return false;
    }

    /**
     * allow admin save
     * @param CUploadedFile $uploadFile
     * @return mixed boolean if upload fail
     *
     */
    public static function saveImage($uploadFile) {
        //allow extensions?
        if (!in_array($uploadFile->extensionName, array('png', 'jpg', 'jpeg', 'bmp', 'gif'))) {
            return false;
        }

        $fileName = time() . '.' . $uploadFile->extensionName;
        if ($uploadFile->saveAs(IMAGE_PATH . $fileName)) {
            //save file successfully
            return $fileName;
        }

        return false;
    }

    /**
     * allow admin save
     * @param CUploadedFile $uploadFile
     * @return mixed boolean if upload fail
     *
     */
    public static function saveContent($uploadFile) {
        //allow extensions?
        if (!in_array($uploadFile->extensionName, array('png', 'jpg', 'jpeg', 'bmp', 'gif'))) {
            return false;
        }

        $fileName = time() . '.' . $uploadFile->extensionName;
        if ($uploadFile->saveAs(CONTENT_PATH . $fileName)) {
            //save file successfully
            return $fileName;
        }

        return false;
    }

    /**
     * allow admin save
     * @param CUploadedFile $uploadFile
     * @return mixed boolean if upload fail
     */
    public static function saveReport($uploadFile){
        //allow extensions?
        if (!in_array(strtolower($uploadFile->extensionName), array('pdf','doc','docx','ppt','pptx','xls','xlsx','xlsb','txt','rar','jpg'))) {
            return false;
        }
        $fileName = time() . '-' . $uploadFile->Name;
        if ($uploadFile->saveAs(REPORT_PATH . $fileName)) {
            //save file successfully
            return $fileName;
        }

        return false;
    }
}